package cn.itcast.stream;

import cn.itcast.entity.UserEntity;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.function.BiConsumer;
import java.util.stream.Stream;

public class sort {

    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三",18));
        userEntities.add(new UserEntity("李四",26));
        userEntities.add(new UserEntity("王二",10));
        userEntities.add(new UserEntity("麻子",28));

        //从小到大排序
        Stream<UserEntity> stream = userEntities.stream();
        stream.sorted(new Comparator<UserEntity>() {
            @Override
            public int compare(UserEntity o1, UserEntity o2) {
                return o1.getAge() - o2.getAge();
            }
        }).forEach(userEntity -> {
            System.out.println(userEntity.toString());
        });

        //从大到小排序
        Stream<UserEntity> stream1 = userEntities.stream();
        stream1.sorted(Comparator.comparingInt(UserEntity::getAge)).forEach(userEntity -> System.out.println(userEntity));
    }
}
